package sol.leetcode;

/**
 * Best Time to Buy and Sell Stock
 */
public class Issue121 {
    public int maxProfit(int[] prices) {
        int max = 0;
        if (prices.length == 0)
            return max;
        int bought = prices[0];
        for (int i = 1; i < prices.length; ++i) {
            if (prices[i] > bought) {
                int delta = prices[i] - bought;
                if (delta > max)
                    max = delta;
            } else {
                bought = prices[i];
            }
        }
        return max;
    }

    public static void main(String[] args) {
        int[] prices = {2, 3, 1, 5, 6};
        Issue121 sol = new Issue121();
        System.out.println(sol.maxProfit(prices));
    }
}
